<template>
  <div class="app-container">
    <!-- <el-button
      @click="dialogFormVisible=true,dialogFormType='add',applicantAccount={}"
      type="primary"
      icon="el-icon-plus"
      plain
    >添加</el-button>
    <el-button
      @click="multipleSelection.length==0?multipleSelectionTip():deleteRow(multipleSelection)"
      type="danger"
      icon="el-icon-delete"
      plain
    >批量删除</el-button> -->
    <el-table
      v-loading="listLoading"
      :data="users"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="index" :index="indexComputed" label="序号" width="55px"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="tel" label="电话号码"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            @click="pass(scope.row)"
          >通过</el-button>
          <el-button
            type="danger"
            size="small"
            @click="notPass(scope.row)"
          >不通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import {getUsers, updateInfo} from '@/api/user'
export default {
  data() {
    return {
      users: [],
      listLoading: true,
      pageSize: 5,
      pageSizes: [5, 10, 15, 20],
      currentPage: 1,
      total: 0,
    }
  },
  created() {
    this.fetchData()
  },
  computed: {
    indexComputed() {
      return (this.currentPage - 1) * this.pageSize + 1
    }
  },
  // watch: {
  //   query: {
  //     deep: true,
  //     handler: function(newVal, oldVal) {
  //       this.fetchData()
  //     }
  //   }
  // },
  methods: {
    async fetchData() {
      this.listLoading = true
      const res = await getUsers({status: 0, pageSize: this.pageSize, pageNum: this.currentPage})
      this.users = res.data.users
      this.total = res.data.total
      this.listLoading = false
    },
    async pass(row){
      row.status = 1
      const res = await updateInfo(row);
      if(res.data != null){
        this.$message({
            message: "审核成功",
            type: 'success'
          })
      }else{
        this.$message({
            message: "审核失败",
            type: 'danger'
          })
      }
      this.fetchData()
    },
    async notPass(row){
      row.status = 2
      const res = await updateInfo(row);
      if(res.data != null){
        this.$message({
            message: "审核成功",
            type: 'success'
          })
      }else{
        this.$message({
            message: "审核失败",
            type: 'danger'
          })
      }
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData();
    },
  }
}
</script>
